﻿@{
   
    Layout = null;
}

<link href="~/libs/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />

<div class="layui-form-item">
    <label class="layui-form-label">单位编码</label>
    <div class="layui-input-inline layui-input-wrap">
        <input type="text" id="unitMeasureCode" name="unitMeasureCode" autocomplete="off" lay-affix="clear" class="layui-input">
    </div>
    <label class="layui-form-label">单位名称</label>
    <div class="layui-input-inline layui-input-wrap">
        <input type="text" id="unitMeasureName" name="unitMeasureName" autocomplete="off" lay-affix="clear" class="layui-input">
    </div>
    <div class="layui-btn-container">
        <button type="button" class="layui-btn layui-btn-sm layui-bg-blue" lay-submit lay-filter="demo-table-search">搜索</button>
        <button type="reset" class="layui-btn layui-btn-sm layui-btn-primary">重置</button>
        <button type="button" class="layui-btn layui-btn-sm layui-btn-normal" lay-on="test-page-custom" onclick="Insert()">
            <i class="layui-icon layui-icon-add-1"></i>新增
        </button>
        <button type="button" class="layui-btn layui-btn-warm layui-btn-sm  layui-bg-red" lay-event="delete">
            <i class="layui-icon layui-icon-delete"></i> 删除
        </button>

    </div>
    <script type="text/html" id="btn">
        <button type="button" class="layui-btn layui-btn-xs layui-btn-normal" lay-event="update" >
            <i class="layui-icon layui-icon-edit"></i> 修改
        </button>
        <button type="button" class="layui-btn layui-btn-xs layui-bg-red" lay-event="delete">
            <i class="layui-icon layui-icon-delete"></i> 删除
        </button>

    </script>
</div>
<table class="layui-hide" id="UnitMeasureLists" lay-filter="UnitMeasureList"></table>

<script src="~/libs/jquery/jquery.js"></script>
<script src="~/libs/layui-v2.5.6/layui/layui.js"></script>

<script>
    layui.use('table', function () {
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var inst = table.render({});
        // 已知数据渲染
        function select() {
            table.render({
                elem: '#UnitMeasureLists',
                url: '/UnitMeasure/GetUnitMeasureList',
                cols: [[ //标题栏
                    { type: 'checkbox', fixed: 'left' },
                    { field: 'unitMeasureCode', title: '单位编码', width: 120 },
                    { field: 'unitMeasureName', title: '单位名称', width: 120 },
                    {
                        field: 'isPrimaryUnit', title: '是否是主单位', width: 120,
                        templet: function (d) {
                            return d.isPrimaryUnit ? "是" : "否"
                        }
                    },

                    { field: 'conversionScale', title: '与主单位换算比例', width: 120 },
                    {
                        field: 'state', title: '是否启用', width: 120,
                        templet: function (d) {
                            return d.state ? "是" : "否"
                        }
                    },
                    { title: '操作', width: 160, toolbar: '#btn' }
                ]],
                page: true, // 是否显示分页
                height: 500,
                limits: [5, 10, 20, 30],
                limit: 10, // 每页默认显示的数量1
                where: {
                    pageIndex: 1,
                    pageSize: 10
                },
            });
        }
        select()

        //查询
        form.on('submit(demo-table-search)', function (data) {
            var fieId = data.fieId;// 获得表单字段
            table.reload('UnitMeasureLists', {
                page: { curr: 1 },// 重新从第 1 页开始加载数据
                where: {
                    unitMeasureCode: $('#unitMeasureCode').val(),
                    unitMeasureName: $('#unitMeasureName').val()// 将表单中的字段作为搜索的条件
                }
            });
            return false;// 阻止表单的默认提交行为
        });

        //删除
        // 触发单元格工具事件
        table.on('tool(UnitMeasureList)', function (obj) { // 双击 toolDouble
            var data = obj.data; // 获得当前行数据
            if (obj.event === 'delete') {
                layer.confirm('确定要删除吗?', function (index) {
                    // 执行删除操作的 AJAX 请求
                    var array = [];  // 声明一个数组，若有多个ID，请相应调整
                    array.push(data.id)
                    console.log(array)
                    deleCust(array);
                    layer.close(index) // 关闭确认框
                })
            }
            if (obj.event === 'update') {
                // 在此处输入 layer 的任意代码
                layer.open({
                    type: 2, // page 层类型
                    area: ['500px', '500px'],
                    title: '编辑单位',
                    shade: 0.6, // 遮罩透明度
                    shadeClose: true, // 点击遮罩区域，关闭弹层

                    content: '/UnitMeasure/UpdateUnitMeasure?id=' + data.id,
                    end: function () {
                        location.reload()
                    }
                });
            }
        });

        // 批量删除按钮点击事件
        $('.layui-btn-warm').on('click', function () {
            var checkStatus = table.checkStatus('UnitMeasureLists');
            var data = checkStatus.data;
            if (data.length === 0) {
                layer.msg('请选择要删除的数据');
                return;
            }
            layer.confirm('确定删除选中的 ' + data.length + ' 条数据吗？', function (index) {
                var ids = data.map(item => item.id);
                deleCust(ids);
                layer.close(index);
            });
        });
        });

    var deleCust = function (ids) {
        $.ajax({
            type: 'get',
            url: '/UnitMeasure/DeleteUnitMeasure',// 替换成实际的删除接口地址
            data: {
                ids:ids.toString()// 将删除需要的参数传递给后端，这里假设删除操作需要 id 参数
            },
            dataType:'json',
            success: function (res) {
                if (res > 0) {
                    layer.msg('删除成功')
                    //le.reload('UnitMeasureLists')
                    location.href = "/UnitMeasure/UnitMeasureList"
                }
                else {
                    layer.msg('删除失败')
                }
            }

        })
    }

    //弹出框
    function Insert() {
        // 在此处输入 layer 的任意代码
        layer.open({
            type: 2, // page 层类型
            area: ['500px', '550px'],
            title: '添加单位',
            shade: 0.6, // 遮罩透明度
            shadeClose: true, // 点击遮罩区域，关闭弹层
            content: '/UnitMeasure/CreateUnitMeasure',
            end: function () {
                location.reload()
            }
        });
    }

</script>
